//
//  UIVIew+Shadow.m
//  GK
//
//  Created by Seven on 2018/12/19.
//  Copyright © 2018 Seven. All rights reserved.
//

#import "UIVIew+Shadow.h"
#include <math.h>
#import <objc/objc.h>
#import <objc/runtime.h>

@implementation UIView(Shadow)


#ifdef DEBUG
#pragma mark - BadBackgroundBehavior
void SEViewAlertForUnsafeBackgroundCalls() {
    printf("----------------------------------------------------------------------------------");
    printf("Background call to setAnimationsEnabled: detected. This method is not thread safe.");
    printf("Set a breakpoint at SEUIViewDidSetAnimationsOffMainThread to inspect this call.");
    printf("----------------------------------------------------------------------------------");
}
+ (void)__setAnimationsEnabled:(BOOL)enabled{
    if (![NSThread isMainThread]) {
        SEViewAlertForUnsafeBackgroundCalls();
    }
    [self __setAnimationsEnabled:enabled];
}
#endif

+ (void)load{
#ifdef DEBUG
    [[UIView class] swizzleClassMethod:@selector(setAnimationsEnabled:) with:@selector(__setAnimationsEnabled:)];
#endif
    swizzleMethod([UIView class], @selector(layoutSubviews), @selector(__layoutSubviews));
}

#pragma mark - 阴影
const NSString *shadowViewKey = @"shadowViewKey";
- (void)set_shadowView:(UIView *)_shadowView{
    objc_setAssociatedObject(self, (__bridge const void *)(shadowViewKey), _shadowView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)_shadowView{
    UIView *tmp = objc_getAssociatedObject(self, (__bridge const void *)(shadowViewKey));
    if (tmp == nil) {
        tmp = [[UIView alloc] init];
        self._shadowView = tmp;
    }
    return tmp;
}
- (UIView *)addShadowView{//默认向下阴影(5, 0, 0, 0)
    return [self addShadowWithColor:[UIColor colorWithWhite:0 alpha:0.12] shadowOffset:CGSizeMake(0, 0) shadowRadius:8 inset:UIEdgeInsetsMake(5, 5, 0, 5)];
}

- (UIView *)addShadowWithColor:(UIColor *)color shadowOffset:(CGSize)shadowOffset shadowRadius:(CGFloat)shadowRadius inset:(UIEdgeInsets)inset{
    return [self addShadowWithColor:color shadowOffset:shadowOffset cornerRadius:shadowRadius inset:inset shadowRadius:8];
}

- (UIView *)addShadowWithColor:(UIColor *)color shadowOffset:(CGSize)shadowOffset cornerRadius:(CGFloat)cornerRadius inset:(UIEdgeInsets)inset shadowRadius:(CGFloat)shadowRadius{
    if (self.superview == nil) {
        return nil;
    }
    CGRect rect = self.frame;
    rect.origin.x += inset.left;
    rect.origin.y += inset.top;
    rect.size.height -= inset.top + inset.bottom;
    rect.size.width -= inset.left + inset.right;
    
    UIView *view = self._shadowView;
    view.frame = rect;
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = self.userInteractionEnabled;
    [self.superview insertSubview:view belowSubview:self];
    view.layer.shadowColor = color.CGColor;
    view.layer.shadowRadius = shadowRadius;
    view.layer.shadowOffset = shadowOffset;
    view.layer.shadowOpacity = .6;
    view.layer.cornerRadius = cornerRadius;
    
    //* view.clipsToBounds = NO;
    view.layer.masksToBounds = NO;
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:view.bounds];
    view.layer.shadowPath = shadowPath.CGPath;
    //*/
    if (![self.superview isKindOfClass:[UITabBar class]]) {
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).insets(inset);
        }];
    }
    /*
     CGRect toRect = [self.superview convertRect:self.frame toView:view];
     self.frame = toRect;
     [view addSubview:self];//*/
    return view;
}

- (UIView *)addSmallShadowView{//
    return [self addShadowWithColor:[UIColor colorWithWhite:0 alpha:.2] shadowOffset:CGSizeMake(0, 0) cornerRadius:5 inset:UIEdgeInsetsMake(0, 0, 0, 0) shadowRadius:10];
}

- (void)addShadow{
    //加阴影
    self.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
    self.layer.shadowOffset = CGSizeMake(0,1);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    self.layer.shadowOpacity = 0.2;//阴影透明度，默认0
    self.layer.shadowRadius = .5;//阴影半径，默认3
}

- (void)__layoutSubviews{
    if (![[NSThread currentThread]isMainThread]) {
        NSLog(@"当前的view 是 %@",self);
    }
    [self __layoutSubviews];
    //*
    UIView *tmp = objc_getAssociatedObject(self, (__bridge const void *)(shadowViewKey));
    if (tmp) {
        UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:tmp.bounds];
        tmp.layer.shadowPath = shadowPath.CGPath;
    }//*/
    if ([NSStringFromClass(self.class) isEqualToString:@"_UIParallaxDimmingView"]) {
        UIImageView *leftEdgeFade = [self valueForKey:@"leftEdgeFade"];//去除滑动时 左边的阴影
        [leftEdgeFade removeFromSuperview];
    }
}

- (void)centerToParent{
    self.center = CGPointMake(self.superview.width/2, self.superview.height/2);
}


#pragma mark AnchorPoint
- (void)setAnchorPoint:(CGPoint)anchorPoint
{
    CGPoint oldOrigin = self.frame.origin;
    self.layer.anchorPoint = anchorPoint;
    CGPoint newOrigin = self.frame.origin;
    
    CGPoint transition;
    transition.x = newOrigin.x - oldOrigin.x;
    transition.y = newOrigin.y - oldOrigin.y;
    
    self.center = CGPointMake (self.center.x - transition.x, self.center.y - transition.y);
}
- (void)setDefaultAnchorPoint{
    [self setAnchorPoint:CGPointMake(0.5f, 0.5f)];
}
@end
